DailyExercise21

First, I downloaded the appropriate files using the code from lecture.

library(dataRetrieval)
Warning: package 'dataRetrieval' was built under R version 4.4.3
library(dplyr)
Warning: package 'dplyr' was built under R version 4.4.3

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(lubridate)
Warning: package 'lubridate' was built under R version 4.4.3

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
library(tsibble)
Warning: package 'tsibble' was built under R version 4.4.3
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'
The following object is masked from 'package:lubridate':

    interval
The following objects are masked from 'package:base':

    intersect, setdiff, union
# Example: Cache la Poudre River at Mouth (USGS site 06752260)
poudre_flow <- readNWISdv(siteNumber = "06752260",    # Download data from USGS for site 06752260
                          parameterCd = "00060",      # Parameter code 00060 = discharge in cfs)
                          startDate = "2013-01-01",   # Set the start date
                          endDate = "2023-12-31") |>  # Set the end date
  renameNWISColumns() |>                              # Rename columns to standard names (e.g., "Flow", "Date")
  mutate(Date = yearmonth(Date)) |>                   # Convert daily Date values into a year-month format (e.g., "2023 Jan")
  group_by(Date) |>                                   # Group the data by the new monthly Date
  summarise(Flow = mean(Flow))                       # Calculate the average daily flow for each month
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31
  1. Convert to tsibble

    library(tsibble)
    
    # Convert the data to a tsibble object
    poudre_flow_tsibble <- as_tsibble(poudre_flow, index = Date)

I used the code above to convert the data.frame into a tsibble object

  1. Plotting the time series

    library(ggplot2)
    Warning: package 'ggplot2' was built under R version 4.4.3
    library(plotly)
    Warning: package 'plotly' was built under R version 4.4.3
    
    Attaching package: 'plotly'
    The following object is masked from 'package:ggplot2':
    
        last_plot
    The following object is masked from 'package:stats':
    
        filter
    The following object is masked from 'package:graphics':
    
        layout
    # Plot the time series using ggplot
    poudre_flow_plot <- ggplot(poudre_flow_tsibble, aes(x = Date, y = Flow)) +
      geom_line() +
      labs(title = "Monthly Average Streamflow", 
           x = "Date", y = "Flow") +
      theme_minimal()
    
    # Animate the plot with plotly
    poudre_flow_plotly <- ggplotly(poudre_flow_plot)
    poudre_flow_plotly

I used the code above to plot the time series data and animate this plot with plotly

  1. subseries

    library(feasts)
    Warning: package 'feasts' was built under R version 4.4.3
    Loading required package: fabletools
    Warning: package 'fabletools' was built under R version 4.4.3
    # Visualize seasonal subseries
    gg_subseries(poudre_flow_tsibble, Flow) +
      labs(title = "Subseries Plot for Cache la Poudre River Streamflow")

I used the code above to visualize the seasonal patterns in the data. In this plot, “seasons” are defined by cycles in the data and “subseries” represent how the data behaves in each year

4.Decompose

library(fable)
Warning: package 'fable' was built under R version 4.4.3
# Decompose the time series using STL
decomposition <- poudre_flow_tsibble %>%
  model(stl = STL(Flow ~ trend(window = 13) + season(window = 13)))

# Plot the decomposition
components <- components(decomposition)
autoplot(components)

I used the code above to decompose the time series data into its components: trend, seasonality, and residuals. In this plot, I see 4 different graphs labeled “Flow”, “trend”, “season_year”, and “remander”. Looking at this graph, the “flow” component is lowest in 2018 and 2020 but increases in subsequent years. Thee “trend” component mimics this pattern. The “season_year” component stays consistant and the “remander” component follows no obvious pattern. I think the “trend” component represents the change in flow over time and the seasonal components represent the time of the year.